import axios from "axios";
axios.defaults.timeout = 2000; //请求超时时间
axios.defaults.baseURL = "http://ceshi5.dishait.cn"; //公共URL

import store from "@/store/index"; //引入vuex

//添加请求拦截器

axios.interceptors.request.use(
  (config) => {
    //添加header头的token
    let token = store.state.token;
    if (token) {
      config.headers.token = token;
    }
    return config;
  },
  (error) => {
    return error;
  }
);

//添加响应拦截器
export function request(methods, url, data) {
  var p = new Promise((resolve, reject) => {
    if (methods == "post") {
      axios
        .post(url, data)
        .then((res) => {
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    } else if (methods == "get") {
      axios
        .get(url)
        .then((res) => {
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    }
  });
  return p;
}
